<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>图片上传</title>
    <header th:replace="~{./common/header.html}"></header>

    <style>
        #imgDiv > img {
            width: 100px;
            padding: 2px;
        }
        .images{
            float: left;
        }
        .images > img{
            width: 200px;
            height: 200px;
            padding: 2px;
        }
    </style>

    <script th:src="@{/static/js/exif.js}" type="text/javascript"></script>
    <script th:src="@{/static/js/uploadManyImage.js}" type="text/javascript"></script>
    <script th:inline="javascript">
        $(function () {
            $("#menu-img-info").addClass("active");
        });
        
        function addImg() {
            $("#fileToUpload").click();
        }

        function uploadCallback(base64Img,suffix) {
            var html = '<img src="'+base64Img+'" onclick="$(this.remove())" suffix="'+suffix+'" />';
            $("#imgDiv").append(html);
        }


        function uploadImg(){
            var base64Arr = [];
            var suffixArr = [];
            $("#imgDiv img").each(function () {
                base64Arr.push($(this).attr("src"));
                suffixArr.push($(this).attr("suffix"));
            });
            $.ajax({
                type: "POST",
                url: "/imgInfo/uploadImg",
                data: {'base64Arr':base64Arr, 'suffixArr':suffixArr},
                dataType:'json',
                success: function(data){
                    if(data.status == 1){
                        location.reload();
                    }else {
                        alert(data.info);
                    }
                }
            });
        }

        function deleteImg(id) {
            $("#deleteModal").modal("show");
            $("#deleteBtn").unbind();
            $("#deleteBtn").on("click",function () {
                $("#deleteModal").modal("hide");
                $.ajax({
                    type: "POST",
                    url: "/imgInfo/deleteImg",
                    data: {'id':id},
                    dataType:'json',
                    success: function(data){
                        if(data.status == 1){
                            location.reload();
                        }else {
                            alert(data.info);
                        }
                    }
                });
            });
        }
    </script>
</head>
<body>
<div class="top" th:insert="~{./common/top.html}"></div>
<div class="left" th:insert="~{./common/left.html}"></div>
<div class="page">
    <div style="width: 100%; border: 1px solid gainsboro;overflow: hidden; padding: 20px;">
        <button type="button" class="btn btn-primary" onclick="addImg();" style="float: right;">添加图片</button>
        <button type="button" class="btn btn-primary" onclick="uploadImg();" style="float: right; margin-top: 60px;margin-right: -90px;">上传图片</button>
        <div id="imgDiv">

        </div>
    </div>

    <br>
    <h5>
        使用Canvas压缩图片、调整IOS系统图片旋转问题，最终以base64的方式上传至后台。支持多图批量上传
    </h5>
    <br>

    <!--/*@thymesVar id="imgInfo" type="com.baomidou.mybatisplus.plugins.Page<com.longzongqin.demo.entity.ImgInfo>"*/-->
    <div th:each="img:${imgInfo.getRecords()}" class="images">
        <img th:src="${img.url}" th:onclick="'deleteImg('+${img.id}+')'">
    </div>



    <div style="display: none;">
        <input id="fileToUpload" multiple="multiple" type="file" onchange="selectFileImage(this,'')" name="fileToUpload" accept="image/*">
    </div>



</div>



<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel1" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel1">添加用户</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
               <h5>确认删除？</h5>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-danger" id="deleteBtn">删除</button>
            </div>
        </div>
    </div>
</div>




</body>
</html>